﻿package apj.demo.message.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.javafx.scene.web.Debugger;

import apj.demo.util.JDBCUtil;

@WebServlet("/change")
public class PageChangeServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		try {
			int addNum = getAddNum();
			
			List<String> nameList = getMsgName(request);
			String id = request.getParameter("num_id");
			List<String> messagesList = searchMsg(nameList, id);
			
			List<String> nameListChange =  changeName(nameList);
			
			request.getSession().setAttribute("work", request.getParameter("work"));
			request.getSession().setAttribute("type", request.getParameter("name"));
			
			
			com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
			json.put("nameList", nameList);
			json.put("nameListChange", nameListChange);
			json.put("messagesList", messagesList);
			json.put("addNum", addNum);
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//获取表列名
	public List<String> getMsgName(HttpServletRequest request) throws Exception{
		List<String> nameList =  null;
		String name = request.getParameter("name");
		switch (name) {
		case "department":
			nameList =  searchName(name);
			break;

		default:
			break;
		}
		return nameList;
	}
	
	public List<String> searchName(String name) throws Exception{
		String sql = "select COLUMN_NAME as name from information_schema.columns where table_name='"+name+"'";
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rst = null;
		List<String> nameList = new ArrayList<String>();
		
		try{
			conn = JDBCUtil.getConnection();
			prep = conn.prepareStatement(sql);
			rst = prep.executeQuery();
			
			while(rst.next()){
				String tname = rst.getString("name");
				
				nameList.add(tname);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			conn.close();
			prep.close();
			rst.close();
		}
		
		return nameList;
	}
	
	public List<String> changeName(List<String> nameList){
		List<String> nameListChange = new ArrayList<String>();
		for(int i=0;i<nameList.size();i++){
			String tname = nameList.get(i);
			switch(tname){
				case "dep_id":
					tname = "部门编号";
					break;
				case "dep_name":
					tname = "部门名称";
					break;
				case "leading_official":
					tname = "部门领导";
					break;
				case "description":
					tname = "部门信息";
					break;
				default:
					break;
			}
			nameListChange.add(tname);
		}
		return nameListChange;
	}
	
	//获取表信息
	public List<String> searchMsg(List<String> nameList,String id) throws Exception{
		List<String> messagesList = new ArrayList<String>();
		String message = "";
		String id_name = nameList.get(0);
		
		for(int i = 0; i < nameList.size();i++){
			if(i == 0){
				message = id;
				messagesList.add(message);
			}else{
				String tname = nameList.get(i);
				String sql = "select "+ tname +" from department where " + id_name +" = " + id;
				Connection conn = null;
				PreparedStatement prep = null;
				ResultSet rst = null;
				
				try{
					conn = JDBCUtil.getConnection();
					prep = conn.prepareStatement(sql);
					rst = prep.executeQuery();
					
					if(rst.next()){
					message = rst.getString(tname);
					}
					
					messagesList.add(message);
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					conn.close();
					prep.close();
					rst.close();
				}
			}
			
		}
		return messagesList;
	}
	
	//获取添加的id
	public int getAddNum() throws Exception{
		String sql = "select count(*) as addnum from department";
		
		int addNum = 0;
		
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rst = null;
		
		try{
			conn = JDBCUtil.getConnection();
			prep = conn.prepareStatement(sql);
			rst = prep.executeQuery();
			
			if(rst.next()){
				addNum = rst.getInt("addnum");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			conn.close();
			prep.close();
			rst.close();
		}
		
		return addNum+1;
	}
	
}
